/**
 * 
 */
package ads.engenharia.dal;

import java.lang.reflect.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import ads.engenharia.entity.Course;
import ads.engenharia.entity.Professor;

/**
 * @author Lanedraex
 *
 */
public class ProfessorDAO {

	private String sql;
	private String tableName = "Professor";

	private EntityManager em = new EntityManager() {

		@Override
		public Object treatResultSet(ResultSet resultSet) throws SQLException {
			Professor professor = new Professor();
			professor.id(resultSet.getInt("id"));
			professor.name(resultSet.getString("name"));
			professor.workload(resultSet.getInt("workload"));
			professor.isFree(resultSet.getBoolean("is_free"));

			return professor;
		}
	};

	/**
	 * Salva e retorna o id.
	 * 
	 * @param professor
	 * @return id
	 * @throws SQLException
	 */
	public int save(Professor professor) throws SQLException {

		sql = "insert into " + tableName + " ( name, workload, is_free )"
				+ " values ( ?, ?, ? ) returning id ";

		return (int) em.getSingleResult(sql, professor.name(),
				professor.workload(), professor.isFree());
	}

	/**
	 * Deletes professor.
	 * 
	 * @param professor
	 * @throws SQLException
	 */
	public void delete(Professor professor) throws SQLException {

		sql = " delete from " + tableName + " where id = ? ";

		em.executeQuery(sql, professor.id());
	}

	/**
	 * Deletes all professors.
	 * 
	 * @throws SQLException
	 */
	public void deleteAll() throws SQLException {

		sql = " delete from " + tableName;

		em.executeQuery(sql);
	}

	/**
	 * Updates professor.
	 * 
	 * @param professor
	 * @throws SQLException
	 */
	public void update(Professor professor) throws SQLException {

		sql = " update " + tableName
				+ " set name = ?, workload = ?, is_free = ? where id = ? ";

		em.executeQuery(sql, professor.name(), professor.workload(),
				professor.isFree(), professor.id());
	}

	/**
	 * Searches for professor.
	 * 
	 * @param professor
	 * @return professor
	 * @throws SQLException
	 */
	public Professor search(Professor professor) throws SQLException {

		sql = " select id, name, workload, is_free from " + tableName
				+ "  where id = ? ";

		return (Professor) em.getSingleResult(sql, professor.id());
	}

	/**
	 * Searches all professors.
	 * 
	 * @return List of professors.
	 * @throws SQLException
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<Professor> searchAll() throws SQLException {

		sql = " select id, name, workload, is_free from " + tableName;

		return (ArrayList<Professor>) em.getResultList(sql);
	}

	/**
	 * 
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public Professor searchById(int id) throws SQLException {

		sql = " select id, name, workload, is_free from " + tableName
				+ "  where id = ? ";

		return (Professor) em.getSingleResult(sql, id);
	}

	/**
	 * 
	 * @return
	 * @throws SQLException
	 */
	public Professor searchByMaxId() throws SQLException {

		sql = " select id, name, workload, is_free from " + tableName
				+ "  where id = ( select max(id) from " + tableName + " ) ";

		return (Professor) em.getSingleResult(sql);
	}

	/**
	 * 
	 * @param name
	 * @return
	 * @throws SQLException
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<Professor> searchByName(String name) throws SQLException {

		sql = " select id, name, workload, is_free from " + tableName
				+ "  where name = ? ";

		return (ArrayList<Professor>) em.getResultList(sql, name);
	}

	/**
	 * 
	 * @param isFree
	 * @return
	 * @throws SQLException
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<Professor> searchByStatus(boolean isFree)
			throws SQLException {

		sql = " select id, name, workload, is_free from " + tableName
				+ "  where is_free = ? ";

		return (ArrayList<Professor>) em.getResultList(sql, isFree);
	}

}
